package com.renge.statisticsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.commonutils.R;
import com.renge.commonutils.RandomUtil;
import com.renge.statisticsservice.client.UcenterMember;
import com.renge.statisticsservice.entity.StatisticsDaily;
import com.renge.statisticsservice.entity.vo.AnalysisVo;
import com.renge.statisticsservice.mapper.StatisticsDailyMapper;
import com.renge.statisticsservice.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-02
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterMember ucenterMember;
    //把分析的数据加入到表格
    @Override
    public void addStatisticsToTable(String day) {
        //判断是否存在此天数，存在先删除
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        this.remove(wrapper);
        //插入最新的数据
        R r = ucenterMember.checkDayByCode(day);
        Integer dayCode = (Integer)r.getData().get("dayCode");
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(day);  //每天
        statisticsDaily.setRegisterNum(dayCode); //记录每天的注册人数
        statisticsDaily.setCourseNum(Integer.valueOf(RandomUtil.getTwoBitRandom()));//记录每天的新增课程人数
        statisticsDaily.setLoginNum(Integer.valueOf(RandomUtil.getTwoBitRandom()));//记录每天的登录人数
        statisticsDaily.setVideoViewNum(Integer.valueOf(RandomUtil.getTwoBitRandom()));//记录每天的看视频人数
        this.save(statisticsDaily);
    }
    //分析数据返回图表
    @Override
    public Map<String, Object> checkAnalysisToTable(AnalysisVo analysisVo) {
        //查询类不能为空
       if(!StringUtils.isEmpty(analysisVo)){
           QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
           //查询类型
           if(!StringUtils.isEmpty(analysisVo.getType())){
               wrapper.select(analysisVo.getType(),"date_calculated");
           }
           //时间日期
           if(!StringUtils.isEmpty(analysisVo.getBegin())&&!StringUtils.isEmpty(analysisVo.getEnd())){
                wrapper.between("date_calculated",analysisVo.getBegin(),analysisVo.getEnd());
           }
           List<StatisticsDaily> list = this.list(wrapper);
           //遍历集合
           List<String> sYear = new ArrayList<>();
           List<Integer> sCount = new ArrayList<>();
           if(!StringUtils.isEmpty(list)&&list.size()>0){
               for(StatisticsDaily sd:list){
                   //封装年份
                   sYear.add(sd.getDateCalculated());
                   //封装count值
                   switch (analysisVo.getType()){
                       case "login_num":
                           sCount.add(sd.getLoginNum());
                           break;
                       case "register_num":
                           sCount.add(sd.getRegisterNum());
                           break;
                       case "video_view_num":
                           sCount.add(sd.getVideoViewNum());
                           break;
                       case "course_num":
                           sCount.add(sd.getCourseNum());
                           break;
                        default:
                            break;
                   }

               }
           }
           HashMap<String, Object> map = new HashMap<>();
           map.put("sYear",sYear);
           map.put("sCount",sCount);
           return map;
       }

        return null;
    }
}
